package com.zues.thread.create.pool;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 每周四18:00:00定时执行任务
 * @ClassName TestSchedule
 * @Description TODO
 * @Author zeus
 * @Date 3/24/2025 10:47 AM
 * @Version 1.0
 **/
@Slf4j(topic = "c.TestSchedule")
public class TestSchedule {

    public static void main(String[] args) {
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();

        //获取周四时间
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
        //临时测试
//        LocalDateTime time = now.withHour(14).withMinute(32).withSecond(0).withNano(0).with(DayOfWeek.MONDAY);

        //如果 当前时间 > 本周四，必须找到下周周四
        if(now.compareTo(time) > 0){
            time = now.plusWeeks(1);
        }

        //initailDelay 代表当前时间和周四的时间差
        //period 一周的间隔时间
        long initailDelay = Duration.between(now, time).toMillis();
        long period = 1000 * 60 * 60 * 24 * 7;
        //一秒运行一次测试
//        long period = 1000;

        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        pool.scheduleAtFixedRate(()->{
            System.out.println("running...");
        },initailDelay,period, TimeUnit.MILLISECONDS);


    }

}
